I'm trying to make a rewrite rule which will understand
http://example.com/test/1234
as
http://example.com/test.php?t=1234
This is what I have right now and it doesn't work:
RewriteEngine on
RewriteRule ^test?t=([^/\.]+)/?$ http://mywebsite.com/test/$1 [L]
Can someone give me a hand?
Your rewriteRule is backwards in that you're supposed to match on the left what you want the clean url to look like and rewrite on the right to where the file is located on the server. But even if it weren't backwards, you'd have to escape the question mark character in the RegExp. But since it is backwards, you should be using something closer to:
RewriteEngine on
RewriteRule ^test/(.*) test.php?t=$1 [L]
Try :
RewriteEngine On
RewriteCond %{THE_REQUEST} /test.php\?t=([^&\s]+) [NC]
RewriteRule ^test.php$ /test/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([^/]+)/?$ /test.php?t=$1 [QSA,L,NC]
Query string is not part of match in rewrite rule directive, Use %{QUERY_STRING} or %{THE_REQUEST} variables to match against the query string.
Related
Before you flag this question -- I did search for answers, and found one close to mine that was not answered directly, so...
I am trying to write a simple RewriteRule in my .htaccess file to change ONLY the links that match to an SEO-friendly format.
RewriteEngine On
RewriteRule ^/includes/seo.([a-zA-Z]+).php$ /$1
So if I have a file
/includes/seo.mydocument.php
it will appear in the browser as
/mydocument
Not sure what I'm missing.
Leading slash is not matched in .htaccess rules so use this
RewriteRule ^includes/seo\.([a-zA-Z]+)\.php$ /$1 [L,R]
But probably you want this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+includes/seo\.([^.]+)\.php[/\s?] [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /includes/seo.$1.php [L]
I have a very simple rewrite rule. I would like the second parameter in the URL to be optional, but as it stands at the moment i have to pass it in the URL:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
I have the URL /signup/PARAMETER/OPTIONAL-PARAMETER/
How can I make the url work if I leave the second parameter off?
Joe
Just add a single parameter rewrite after it:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
# single parameter
RewriteRule ^signup/([^/]*)/$ /signup/index.php?e=$1 [L]
I used the answer from this question htacess and two post parameters .... Thanks aeshabana for the tip!
I also needed to add a slash to the URLS so here is my final .htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^signup/(.+)/$ /signup/index.php?uri=$1 [QSA,L]
I'm having a hard time having this to work..
I have installed YOURLS wich is a PHP script to shorten urls.
In order to work, it needs to have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
No problem here.
But I also want to use a directory for image hosting that has nothing to do with the PHP script.
It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1
I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]
This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.
The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]
Hate to ask this question, but I've been banging my head on the desk for a while and can't seem to get it. I'm using ExpressionEngine, and I'm using mod_rewrite to remove index.php from all of my URLs. That works fine. Additionally, I want anything that is myurl.com/adwords/(anything) to be rewritten to myurl.com/(anything). My regex and htaccess skillz are weak. Here is what I have in .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/adwords/(.*)$
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So I think I'm saying anything that ends in /adwords/(something), capture the something, and then append it at the end of index.php via $1. I'm guessing this is simple. Thanks!
The rule you are looking for as such is probably simply
RewriteRule ^adwords/(.*)$ index.php/$1 [L]
with no rewrite condition needed, but I wonder... do you really want to rewrite http://myurl.com/adwords/foo to http://myurl.com/index.php/foo, rather than to http://myurl.com/index.php?a=foo ?
If you also want to rewrite something like http://myurl.com/baa/adwords/foo to http://myurl.com/baa/index.php/foo, you have to omit the ^:
RewriteRule adwords/(.*)$ index.php/$1 [L]
BTW, you can test most of your rewrite rules here: http://htaccess.madewithlove.be/
I am having troubles getting my rewrite rules to work correctly..
ErrorDocument 404 /404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^companies/
RewriteRule ^companies\/\?(.*)([A-Za-z]+) index.php?cpy=$1 [R=301,L,NC]
RewriteRule ^([0-9]+)/?$ index.php?cid=$1 [L]
RewriteRule ^([A-Za-z_]+)/?$ index.php?cat=$1 [L]
The rewrite rules for
domain.com/123
domain.com/abc
seem to work ok,
but the other one I cannot get to work is
domain.com/companies/?list=this
It seems that apache doesn't find a match for
RewriteRule ^companies\/\?(.*)([A-Za-z]+) index.php?cpy=$1 [R=301,L,NC]
Can anyone help me figure out what is wrong with that rule? or if it is something else?
RewriteRules do not match on query strings as a rule.
You can match on them using RewriteCond with the parameterized matches appearing as %1, %2, etc.
eg.
RewriteCond %{QUERY_STRING} ^(.*)([A-Za-z]+)$
RewriteRule ^companies\/ index.php?cpy=%1 [R=301,L,NC]