I have url
http://www.domain.com/folder/?variable=1
i would like to do a mod redirect to index.cfm with the folder going in as path variable and the variable as the second variable.. the first three lines work with my site, i am having trouble with the last bit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.cfm?path=$1 [L]
RewriteRule (.*)\?(.*)$ /index.cfm?path=$1&$2 [L]
thanks
You need to add QSA (or qsappend) flag, like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.cfm?path=$1 [L,QSA]
This will make mod-rewrite append any original query-string to the sub-request.
I had problems redirecting a URL with a ? in it.
The url was like: /index/slug_with_questionmark?.html
The solution that works for me:
RewriteRule ^index/slug_with_questionmark(.*) http://yourdomain.com/newurl? [R=301,L]
Pay attention to the ? at the second statement. This one fools the browser so it redirects it nicely.
Related
My URL looks like this:
example.com/index.php?f=directory&s=page
I want it to be like this instead:
example.com/directory/page
my rewrite rules look like this right now:
RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
it appears to work but not quite because if I call the queries in PHP I get something like this:
print $_GET['f'] */ it prints nothing
print $_GET['s'] */ it prints directory/page
which is not what I intend. How can I fix it?
Thank you.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
Do it like this instead:
RewriteRule ^([^/]+)/([^/]+)$ index.php?f=$1&s=$2 [L]
Request /directory/page and it internally rewrites the request to index.php?f=directory&s=page.
By making the regex more specific, the filesystem checks can probably be avoided.
To redirect from index.php?f=directory&s=page to /directory/page (if these URLs have already been indexed and/or linked to by third parties), then add the following redirect before the above directive:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^f=([^&]+)&s=([^&]+)$
RewriteRule ^index\.php$ /%1/%2 [R=301,L]
But you must already be linking to the canonical /directory/page URL in your application.
Test with 302s to avoid potential caching issues. Clear your browser cache before testing.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
You are only assigning a single URL parameter s, so yes, $_GET['f'] will indeed be empty.
i have an index.php in the following folder:
https://example.com/folder1/folder2/index.php
If the URL is
https://example.com/folder1/folder2/93j3h233j3
then redirect to the index.php but the URL should stay the same!
My idea: i call a php file without .php first and then try to redirect...
RewriteRule ^folder1/folder2/([^\.]+)$ /folder1/folder2/index.php?&%{QUERY_STRING}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/folder2/(.*)$ /folder1/folder2/index.php? [R=302]
This doesnt work.
And keep the name! but how?
Any Idea?
Thank you
You can have your rule as this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder1/folder2)/.+$ $1/index.php [L,NC]
There is no need to use your first rule.
I need to simultaneously do two things with htaccess.
I need to take a URL like:
http://client.example.com/123
and rewrite the directory to a param, and simultaneously add another subdomain to the url so it looks like this:
http://client.qa.example.com/?param=123
This does the param bit correctly, but I can't figure out how to add the subdir:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ /?param=$1 [L]
You can examine the host header using a RewriteCond and extract the relevant parts of the name. Use them in the rewrite. Back references to matches in RewriteConds appear as %n
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
(.+?)\.(.*) will do a match on everything up to the first . and then everything to the end. So client and example.com will respectively be in %1 and %2
If your .htaccess is in the root of client.example.com, it should be a simple redirect. Of course the directory has to be a fake directory or this won't redirect.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://client.qa.example.com/?param=$1 [R=301,QSA,L]
You can use the following to match (check for htaccess syntax):
(http://[^.]+\.)([^/]+/)([^/]*)/?$
And replace with:
$1qa.$2?param=$3
See DEMO
Finally got it working using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
Now I just have to figure out how to work in 2 parameters, given that param 2 isn't always going to be present.
I need to write a .htaccess file to rewrite all urls which have anyting after the domain.
For example RewriteRule ^(.*)$ index.php?page=course&course=$1 [L]
But this sends example.com to that rewritten url as well, and I don't want that. I want it to rewrtire it ONLY if these is really something after the domain, like example.com/CITA180.
I know that I could do example.com/c/CITA180 and then do RewriteRule ^c/(.*)$ index.php?page=course&course=$1 [L] but I don't want to do it like that if I don't have to.
Thanks.
You can use .+ instead of .* to make sure it doesn't match landing page. You will also need RewriteCond %{REQUEST_FILENAME} !-f condition to avoid matching default landing page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=course&course=$1 [L]
Add regex condition: RewriteCond %{REQUEST_FILENAME} REGEX
In your case: RewriteCond %{REQUEST_FILENAME} [A-Z]
This will check if it contains any character or not.
Along with not a file check: RewriteCond %{REQUEST_FILENAME} !-f
i want to change a url like : localhost/site/home.php?p=index to localhost/site/index
i use this code in my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
but when i write like localhost/site/home.php?p=profile.user i get the 404 error, and go to this link
localhost/profile.user
so how can i fix itthanks
Let's look at your rewrites first:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
This is relative rewrite: the replacement text home.php... does not begin with a slash. Relative rewrites in a per-directory context (<Directory> or .htaccess) require a RewriteBase directive to be configured, otherwise they do the wrong thing.
Secondly, your rule is backwards, If you want to rewrite the home.php URL to the site/index one, you have to put the home.php match on the left side, and the site/index on the right:
RewriteRule ^home.php?p=(.*) /site/$1
Notice that I have an absolute rewrite. This means that mod_rewrite will create a URL out of the rewrite by sticking http://example.com on it. A new request is internally generated now for http://example.com/site/<whatever>. We can get away without using RewriteBase since we have no relative rewrites.
As for your last question, it is not clear why when you access localhost/site/home.php?p=profile.user you're being taken to localhost/profile.user. I'm suspecting that it's your home.php script doing that, perhaps. You're trying to use mod_rewrite to hijack that particular kind of PHP request and send it elsewhere, right?
What you meant is probably: you want to rewrite this way:
http://mysite.com/index => http://mysite.com/home.php?p=index
So this should work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?$ /home.php?p=$1 [QSA,L]
Now if you want the opposite:
http://mysite.com/home.php?p=index => http://mysite.com/index
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /home\.php$ / [QSA,L]