Viewed   57 times

I'm trying to build a url shortener, and I want to be able to take any characters immediately after the domain and have them passed as a variable url. So for example

  • http://google.com/asdf

would become

  • http://www.google.com/?url=asdf.

Here's what I have for mod_rewrite right now, but I keep getting a 400 Bad Request:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*) index.php?url=$1 [L,QSA]  

 Answers

3

Try replacing ^(.*) with ^(.*)$

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Edit: Try replacing index.php with /index.php

RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
Wednesday, November 2, 2022
5

I've found a solution:

RewriteEngine on
RewriteBase /mysite/

RewriteRule ^css/css.css css/css.php [L]
RewriteRule ^css/(.*)$ css/$1 [L]
RewriteRule ^js/js.js js/js.php [L]
RewriteRule ^js/(.*)$ js/$1 [L]
RewriteRule ^img/(.*)$ img/$1 [L]
RewriteRule ^(.*)$ index.php?rewrite=$1 

It works fine, but I don't know why it's necessary

RewriteRule ^css/(.*)$ css/$1 [L]

and

RewriteRule ^js/(.*)$ js/$1 [L]

I hope it hepls anyone.

Thanks! :)

Thursday, December 1, 2022
 
4

Here is the code for your .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^user/([^/]*)/([^/]*)$ profile.php?username=$1&page=$2 [NC,L]
Monday, August 29, 2022
 
3

This works for me:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

If the traffic is coming in over non-SSL HTTP, then redirect to the HTTP equivalent of whatever page the user was originally trying to access. It also doesn't involve any mod_rewrite options, so it's easy to read.

Side rant: why does everyone feel the need to explicitly set the HTTP code of the redirect and mark one of their rewrites as the "last" one? Seriously, I've seen dozens of same-looking htaccess rules in just the last few days.

Wednesday, November 30, 2022
 
3

You can use mod_rewrite for this.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index.html$ index.html [L,R=410]

This rule will rewrite requests to non-existing files to index.html and send the 410 status code along with the response. But this requires Apache 2 as R=4xx is only available since Apache 2.

Saturday, December 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :