I want a simple redirect in my .htaccess, with the goal of making a "shortlink" to a long URL.
mydomain.com/short
to take the user to
http://www.mydomain.com/blahblah/foo/bar/foobar/uglylongurl.html
So I tried this:
Redirect /short http://www.mydomain.com/blahblah/foo/bar/foobar/uglylongurl.html
but within the same .htaccess file is:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is causing my simple redirect to have "short" appended as a query string.
I have tried [R] to redirect immediately and I've also tried [L] to stop processing if the first (simplest) rule is used. Both give me a 500 error.
I hope someone knows what I'm missing here. I am on a tight deadline and this is just killing me :P Thanks in advance for any help.
Many thanks to the responder who got this working. I had the redirect above the other rules, however, I needed to change it to a RewriteRule and add the additional code as in his example.
One more issue arose after this....and with his suggestion, I am adding the next layer of the problem to this question (instead of to the comment reply, where the code tags didn't work and it was hard to read).
So here is my next issue. The first one in the list works just fine, whether redirecting to an internal page or an external URL. But subsequent rules give me a 404 error. Here is what it looks like (and note they are all before the one that appends the query string):
RewriteRule ^short/?$ /ugly/long/url.html [L,NC]
RewriteRule ^/sweet/?$ /another/ugly/long/url.html [L,NC]
RewriteRule ^/offsite/?$ http://www.somewhereelse.com/with/a/long/url.html [L,NC]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Order of rewrite rules in pretty important. First have your desired rule then rest of the rules.
RewriteEngine On
RewriteRule ^short/?$ /blahblah/foo/bar/foobar/uglylongurl.html [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Related
My requirement is
domain.com/src/home.php?page=category&category=recipe
domain.com/src/home.php?page=article&key=myarticle
domain.com/src/home.php
Expected end url
domain.com/src/category/recipe
domain.com/src/article/myarticle
domain.com
I have written one
RewriteRule ^src/([A-Za-z0-9-]+)/?$ src/home.php?page=article&key=$1 [NC,L]
The problem is if one is working then the other is not.
Can anyone please suggest how to make it work with some condition based on "page" query param.
Thanks in advance.
With your shown samples, attempts, please try following htaccess Rules file. Make sure to keep your htaccess file along with src folder(not inside it).
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]
Yes, I checked the related questions posted in SO, but could not find something that would help me.
My .htaccess has plenty of redirects already, and they work fine, but this one is giving me the go around.
I had a URL like:
http://example.com/comp_all.php?vid_mod=529
which I changed to a more friendly one:
http://example.com/comparatif-voiture/Audi/A4/529
In order to accomplish that I added the following rule within .htaccess:
RewriteCond %{REQUEST_URI} ^/?comparatif-voiture [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9]+$) /comp_all.php?vid_mod=$1 [L]
And that works fine, as well.
Now, I want to have the 'old and ugly' URL still sitting out there to be redirect to the 'nice' ones.
I tried the following:
Redirect 301 /comp_all.php?vid_mod=529 http://example.com/comparatif-voiture/Audi/A4/529
But that does not work. It just shows the 'ugly' URL.
It does not matter whether I placed the above redirect before or after the Rewrite Rule.
You cannot use query string in Redirect directive. You need RewriteCond in mod_rewrite like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+comp_all\.php\?vid_mod=([0-9]+) [NC]
RewriteRule ^ /comparatif-voiture/Audi/A4/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^comparatif-voiture/.+?/([0-9]+)/?$ /comp_all.php?vid_mod=$1 [L,QSA]
PS: I have also simplified your other rule.
i have a special situation and i can't find a good solution. I've already seen dozens of questions/answers here but none of them seems to solve my problem!
I have an url like this:
https://subdomain.domain.com/{user-name}/{app-name}/
"user-name" and "app-name" can change everytime and i need to redirect it to
index.php?u={user-name}&a={app-name}
But if the url is only https://subdomain.domain.com/{user-name}/ i need to redirect it to
store-list.php?u={user-name} (to show a list of all available apps for that user).
My .htaccess is currently like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
It redirects perfectly on both situations but every call to a "real" file doesn't work (for example a call to a js or css file inside my html).
What am i doing wrong??
Making one RewriteCond Set Apply to Several Rules
Yes, we're really close... but a RewriteCond only applies to one rule. That's what is throwing us off. Let's use some tricky logic and put the conditions in reverse:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,S=2]
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
The conditions say that if the files do exist, leave them unchanged, and to skip the two next rules (S=2).
I never want index.php to show up in my URL, even if the user inputs it. Is this possible?
This is variation whatever after several tries. I've come close a few times but this is where it's at for now.
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} index\.php$ //If URL ends in index.php
RewriteRule (.*)index\.php $1 //Somehow remove index.php from the url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Currently I have permalink set up where if the user enters domain.com/sub-dir/my-perma-lin/ it generates a string on the page based on my-perma-link to look like My Perma Link. What I'd like is if the user submits any URL ending in index.php it just removes that from the URL but leaves everything else as is.
domain.com/sub-dir/index.php --> domain.com/sub-dir/
domain.com/sub-dir/my-perma-link/index.php --> domain.com/sub-dir/my-perma-link
I've written quite a few rules in http://htaccess.madewithlove.be/ that work perfectly but when I upload it (to Dreamhost) nothing works.
This for example should work according to the the tester
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} \.php //Not needed but thought it would/should help
RewriteRule (.*)(index\.php)+ $1 [L,R=301,NC]
But it just removes everything after /sub-dir/
I'm either missing something super obvious or it's not possible ...
You need to add some flags to your rule:
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
You can ditch the RewriteCond %{REQUEST_URI} index\.php$ condition, as that's being checked by the regex in the RewriteRule. You need to include a $ at the end of the regex, and include the flags L to stop rewriting and R=301 to redirect.
I'm new to playing with .htaccess for nicely formatted urls and I'm just not sure I'm doing it right.
My current .htaccess file looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php
What I want is for mysite.com/home/56/page-title to go to index.php where I filter out the number and load the correct index page, which works fine.
Before that, I want to check if the url is pointing at search.php, and if so redirect to mysite.com/search/the-search-term, this also works fine.
What isn't working is if I try to visit a specific .php file say mysite.com/control_panel.php - it just takes me to index.php, but I thought that the following line stopped that from happening?
RewriteCond %{REQUEST_FILENAME} !-f
If someone could explain it to me that would be great :)
Thanks!
1. Order of rules matters
2. RewriteCond directives will only be applied to the ONE RewriteRule that follows it. If you need to apply the same conditions to multiple rules you have to write them multiple times or change the rewrite processing logic (multiple approaches available).
Try this one:
RewriteEngine On
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php [L]