I want to redirect this:
foo.com/xxx
... to this:
foo.com/somepage.php?id=xxx
This is how I do it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ http://foo.com/somepage.php?id=$1 [L]
the problem now, is that foo.com doesn't work any more.
I can't see foo.com neither foo.com/index.php
help me! :)
RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]
Be careful tho, foo.com/1 is a BAD move on SEO.
1) You don't need the RewriteCond line. It's intended to determine WHEN your rule applies, not WHICH PART of the request.
[EDIT] UNLESS, your RewriteCond is there to make this rule apply whenever the query string is empty. If that's it, then there's nothing wrong with it.
2) I think you need to include the first / in your rule, like this:
RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]
[EDIT] Both your version and mine will match your index.php file, which might explain why your site is broken right now. If you don't want to match php files, you could add another condition like
RewriteCond ${REQUEST_URI} !^.*\.php$
3) For a rule like this, it might be helpful to add /? at the end (outside the parens, before the $), in case they look for foot.com/xxx/, which makes sense if you want it to look like a directory.
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(xxx=)$ /somepage.php?id=$1 [L]
Of course you didn't clearly specify what xxx= is...
Related
My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]
I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.
I've done things very similar before but for some reason I'm having a little difficulty with the specific scenario. I want to pass a folder path as a variable and make it look pretty.
I have a working url like:
http://mysite.com/albums/index.php?p=folder/subfolder/
I can view it without the 'index.php' like:
http://mysite.com/albums/?p=folder/subfolder/
What I want is a pretty url that looks like this:
http://mysite.com/albums/folder/subfolder/
Basically, anything after /albums/ should be a single variable. I've played with my .htaccess RewriteRule a bunch and can't seem to get it working. (get 404 errors) This is what I currently have:
RewriteEngine On
RewriteRule ^albums/(.*)$ albums/?p=$1
below is what i use though every call is directed to my index.php file and from there i do anything with it
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
Hope it helps
Change (.*) into (.+) to ensure there is at least one character after / , otherwise your rule loops...
alright... so I have this css bundler, using the following link:
http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
Parameter b is the css folder url and parameter f is the css files to get.
However, to help out my cache, I want the question mark gone. If possible, something like:
http://www.example.com/min/b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
I tried the following:
RewriteEngine On
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
Needless to say, It did not work.
Your rules are going to loop, you can try adding a condition in front of your rule so that it looks like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
But it seems, based on your example URLs, you want something more like this:
RewriteRule ^min/b=(.*)$ index.php?b=$1 [L]
This looks like an unusual thing to do where you want to get rid of ? from query string but want to keep ampersand i.e. &.
Anyway if that's what you really want then you can put this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^min/b=([^;]*);f=(.*)$ min/?b=$1&f=$2 [L]
This rule will internally redirect http://www.example.com/min/b=wp-content/themes/mytheme;f=style.css,boxes.css,mods.css,scripts.css to http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.