Asked  7 Months ago    Answers:  5   Viewed   32 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

97

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, March 31, 2021
 
TheLovelySausage
answered 7 Months ago
97

Do you have mod_negotiation installed? If so, that'll take your request and try to work out what file you really meant. Unfortunately mod_negotiation gets confused by the reroute-magic from mod_rewrite, so make sure you disable mod_negotiation's MultiViews option when you try to rewrite the request.

If your overwrite settings allow it, you can disable MultiViews with: Options -MultiViews in your .htaccess file.

Saturday, May 29, 2021
 
Gigamegs
answered 5 Months ago
73

If you want to map the requested paths based on the domain name, then you can use an additional RewriteCond and match the HTTP_HOST first.

You will have to multiply your block of existing RewriteConds and the RewriteRule for each domain - except for the last pair which could just be a default:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^dev.domain.com
RewriteRule ^(.*)$ index.php/dev/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^api.domain.com
RewriteRule ^(.*)$ index.php/api/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/front/$1 [L]

Not tested. But see also the article on serverfault Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?, which explains the interactions between conditions and rewrite rules.

Saturday, May 29, 2021
 
jakubos
answered 5 Months ago
42

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.

Monday, June 14, 2021
 
CBroe
answered 5 Months ago
24

From http://www.webconfs.com/how-to-redirect-a-webpage.php I'd say you can use the following configuration

Don't redirect subfolders/files (as you wanted): www.example.com/demo/ -> www.newexampledomain.com

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Redirect to subfolders/files: www.example.com/demo/ -> www.newexampledomain.com/demo/

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Tuesday, June 29, 2021
 
Kevin_Kinsey
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :