I've established a semi-working .htaccess file which alters this link:
localhost/profile.php?id=6
to this:
localhost/profile/6
However, this is adding .php to the end of my $_GET variables. I'm not very familiar with rewrite modules, .htaccess etc but this is very annoying. Couldn't find anything on the internet about it, if there is a different way to do it or fix it I would greatly appreciate any input.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*) profile.php?id=$1
</IfModule>
Try restricting to only numbers:
RewriteRule ^profile/(\d*) profile.php?id=$1
Or exclude ., if the ID is not certainly numeric:
RewriteRule ^profile/([^.]*) profile.php?id=$1
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 am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome
I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.
I am editing a script and need to read GET variables to make my job done. But it seems the .htaccess file is manipulating it and removes everything at the end of custom URLs. I have no idea how to modify apache configurations to make it to works fine for the script yet me.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
Here is a sample of URL I need to call:
http://domain.tld/controller/plugin/function/route?k1=v1&k2=v2
and the $_GET only contains one 'route' key with the value below:
controller/plugin/function/route
And other query strings are missed. What should I do for having them?
You probably want the QSA: Query String Append rewrite flag which does what it says on the tin.
For example:
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L,QSA]
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