.htaccess rewriting GET - .htaccess

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]

You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

Related

htaccess RewriteRules Strange behavior

Soo i have this htaccess file in my subfolder for a project:
RewriteEngine On
# HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R]
RewriteRule ^homepage$ index.php [L]
RewriteRule ^news$ news.php [L]
RewriteRule ^store$ store.php [L]
RewriteRule ^staff$ teams.php?t=1 [L]
In this file "homepage" works, "news" works and "staff" too but "store" not
I had a similar problem last week but i abandoned it becouse i tried everything:
RewriteRule ^/?news/(.*)$ news.php?art=$1 [L]
Here i want a rewrite rule /news/foobar to /news.php?art=foobar
I know i maybe not understand htaccess fully but if some work why others not?
It can be usefull information i use LiteSpeed Webserver
Soo this is just an answer who has this same problem when you use cyberpanel/openlitespeed you need to restart lsws everytime when you modify your .htaccess only then it will work.
If you use OLS Enterprise you dont need to do this...

RewriteRule for 'pretty links' not working

I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]

301 redirect specific URLS if ?noRedirect not in URL

I'm running a wordpress installation and want to move specific feeds off-site. I've already got most of the technology down, but here's the problem. I want the following URL:
http://www.csicon.net/g/feed
to redirect to
http://feed.mesr.it/g
But if the URL comes in like this:
http://www.csicon.net/g/feed?noRedirect
I don't want it to redirect but load the original. Any thoughts?
The .htaccess file on http://www.csicon.net/ would contain:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\/g\/feed$ http://feed.mesr.it/g [L,R=301]
Note: It has not been tested, but you get the idea.
Later Edit: Also, this can be done in PHP.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)csicon\.net$ [NC]
RewriteCond %{QUERY_STRING} !(^|&)noRedirect [NC]
RewriteRule ^([^/]+)/feed/?$ http://feed.mesr.it/$1 [L,NC,R=302]

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

htaccess rewrite if no rewrite was provided

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

Resources