I am currently creating a site in localhost and have no luck in getting redirect working. I want to redirect
http://localhost/s/profile.php?uid=16
to
http://localhost/s/16
How could I go about doing this. I have tried using examples online with no luck.
Edit: I tried
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /s/profile.php?uid=$1 [L]
Using copy and paste from online. I always try to figure out who to use htaccess but can't seem to do so.
Here is the base functionality you are looking for. This is just a simple example and you should certainly do your research on this subject.
Alternatively, there are plenty of generators.
http://www.htaccessredirect.net/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile\.php$ - [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /profile.php [L,NE]
</IfModule>
You can have this rule
RewriteRule ^s/([^/]*)/?$ /s/profile.php?uid=$1 [L]
In the htaccess file in your "s" folder, try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+s/profile\.php\?uid=([^&\ ]+)
RewriteRule ^ /s/%1.php? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)\.php$ /s/profile.php?uid=$1 [L]
Related
I have multiple php pages like this:
http://www.example.com/si/article.php?id=122&nTitle=my-title
http://www.example.com/si/post.php?id=352&pTitle=my-post-title
i would like to rewrite them to
http://www.example.com/si/122/my-title
http://www.example.com/si/352/my-post-title
I tried different things in my .htaccess file and no result. Please tell me how to make to work with multiple rules. If I use one rule in .htaccess file its work for one php page like below:
RewriteRule ^([\s\w-]+)/(.*)/?$ article.php?id=$1&nTitle=$2 [L,QSA,NC]
This is now I tried it for multiple pages:
<ifModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /article.php?id=$1&pName=$2 [L,QSA,NC]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /post.php?id=$1&pName=$2 [L,QSA,NC]
</IfModule>
Can anyone give me an advice on how to use this functionalities in .htaccess?
Thank you.
You can use url like : (look like different from each other)
http://www.example.com/arical-si/122/my-title
http://www.example.com/post-si/352/my-post-title
then redirect to :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$0
RewriteRule ^artical-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ article.php?id=$1&nTitle=$2 [L]
RewriteRule ^post-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ post.php?id=$1&nTitle=$2 [L]
</IfModule>
First of all you should test your regex, you can do it there regex generator
Then in your htaccess, you can put your regex like this:
RewriteEngine on
RewriteRule ^/post.php?id=([0-9])&pName=([a-z]+)$ post/$1/$2 [L]
RewriteRule ^/article.php?id=([0-9])&pName=([a-z]+)$ article/$1/$2 [L]
try it step by step and you can debug it easily.
I know that this question has been asked a lot, but nothing I've seen so far worked.
I have a wp in root, running from subdirectory (I like things clean). I also a few subdomains running from subdirectories in root (eg. public_html/sample.com). I get the error only on subdomains that are pure HTML and CSS. They don't use any platform.
Here's my .htaccess from root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Things I've tried:
1) Comment out all RewriteRule - solves the problem, but then my wp doesn't work. It gives 404 error when I go to any posts or pages.
2) Changed the .htaccess to the following (courtesy of Scott Yang):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
</IfModule>
but that didn't change anything.
3) I also tried commenting out and pasting new code from other questions, quite on random, but it's the same story.
Any ideas?
Cheers
I've found a solution.
In the root directory for the subdomain, in the .htaccess add the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
Rewrite Rule ^([^.]+)$ $1.php [NC,L]
This is how you can get rid of .php from the end of URL.
Replace .php with .html and it also gets rid of .html
For some reason, this fixes the problem.
Cheers
EDIT:
This solved the problem, but it didn't remove the .html from the URL. Below worked perfectly
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Cheers
I am new in codeigniter and i am trying to remove index.php from URL in Codeigniter but not able to do this.
I wrote code in my .htaccess file-
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My Apache mod_rewrite is enable.
But still got 404 error when i am trying to access any controller without using index.php.
Please help.
Thanks.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
</IfModule>
Is the code I'm using, it works on every hosting platform I've used it on.
This should help you,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
check this code of htaccess file.
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|css|js|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
you can find your answer refference here
My .htaccess file is not working
moreover can you share your base url with us? as this error may be due to wrong baseurl set.
My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations
I want to make my first api, but im having trouble setting up the urls. The api url is here:
http://tools.fifaguide.com/api/
So for example if some one goes to:
http://tools.fifaguide.com/api/player/messi
Then I need this page to be loaded:
http://tools.fifaguide.com/api/server.php
What do I write in .htaccess? and where should I put it?
This is what I have right now:
RewriteEngine On
RewriteRule ^api http://tools.fifaguide.com/api/server.php
But it doesnt do anything, also the htacces file is in the api folder.
Any help would be really apreciated!
This is what ended up working for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ api/index.php [QSA,L]
////// wordpress stuff /////////
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You might try this:
RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php [R,L]
And if you need the resulting url you could add this:
RewriteEngine On
RewriteRule ^api/(.*) http://tools.fifaguide.com/api/server.php?r=$1 [R,L]
Then in your script you could access the requested url with $_GET["r"] (assuming php...)
Also a helpful tool i've found for htaccess:
http://htaccess.madewithlove.be/
-Ken