page not found with two parameter htaccess rewrite - .htaccess

Hello I am cleaning up my urls by using the htaccess file.
I have a parameter called: page and a parameter called: id.
so my original url is:
http://bouwen040.sayhey.nl/index.php?page=leden&id=15
and I would like it to be:
http://bouwen040.sayhey.nl/leden/15
the page that calls only the 'page' parameter works. The other one is giving a page not found error.
this is my rewriterule code:
RewriteEngine On
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9\-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9\-]+)/$ index.php?page=$1&id=$2
I also tried this one:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /index.php?page=$1&id=$2 [L]
I just don't know why one parameter works and two doesn't ?

Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]

Related

Using multiple rewrite rules

i'm trying to create multiple rewrite rules to make friendly URL but what i did, makes my website throw error 500.
I've tried this but can't seem to make it work.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*) /index.php?category=$1 [L]
RewriteRule ^(.*)/(.*) /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^(.*)/(.*)/(.*) /index.php?category=$1&subcategory=$2&userid=$3 [L]
What i need is basically to make this urls work:
domain.com/GetAnnouncements as domain.com/index.php?category=GetAnnouncements
domain.com/Persona/GetAchievements/2 as domain.com/index.php?category=Persona&subcategory=GetAchievements&userid=2
and there also should be third option that works 'in between' without 3rd parameter which is &userid=2
With your shown samples please try following .htaccess rules file.
Make sure to use either 1st OR 2nd solution only at a time.
Please make sure:
To keep your .htaccess rules file, index.php file in your root location.
Clear your browser cache before testing your URLs.
1st solution: Generic rules where using regex.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)/?$ /index.php?category=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?category=$1&subcategory=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [L]
OR 2nd solution: Using specific string/URLs only as per your shown samples.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(GetAnnouncements)/?$ /index.php?category=$1 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)/?$ /index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^(Persona)/(GetAchievements/)(/d+)/?$ /index.php?category=$1&subcategory=$2&userid=$3 [NC,L]
After some more googling and consulting with my friend we came to this solution which works:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)$ index.php?category=$1&subcategory=$2&userid=$3 [L,QSA]
RewriteRule ^(.*)/(.*)$ index.php?category=$1&subcategory=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
Thank you everyone who tried to help!

Remove index.php keep parameter only

I have this url http://subdomain.domain.com/index.php?page=print-items and I want to remove index.php?page= and only keep what comes after it.
I tried this but it didn't work. it only removes index.php and keeps ?page=print-items
RewriteEngine On
RewriteRule ^([a-z0-9]+) index.php?page=$1
Could you please try following, written and tested as per shown samples.
RewriteEngine ON
RewriteCond %{THE_REQUEST} index\.html\s [NC]
RewriteRule ^(.*/?)/?$ index.html?page=$1 [NC,L]
OR if you are hitting usre friendly URL and you want to redirect in backend into index.html URL with query string then try following.
RewriteEngine ON
RewriteCond %{THE_REQUEST} !index\.html\s [NC]
RewriteRule ^(.*/?)/?$ index.html?page=$1 [NC,L]

301 Redirect - variable in the old url

I have several urls on a Joomla site which have been indexed and I need to 301 redirect them into some new pages. The old URL is formed like this:
http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=20
I want it to go to:
http://www.mydomain.com/en/family-members/family-disease
I tried using:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^/en/wfmenuconfig/family/family-disease/177-category-english$ http://www.www.mydoamin.com/en/family-members/family-disease%1 [R=301,L]
I've tried several answers on here but nothing seems to be working.
htaccess 301 redirect dynamic url
and
301 Redirecting URLs based on GET variables in .htaccess
Any ideas what I should try next? (I've tried a normal redirect 301)
You've almost got it. You need to remove the leading slash from your rule's pattern because it's removed from the URI when applying rules from an htaccess file:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease%1? [R=301,L]
You also don't need the http://www.www.mydoamin.com bit (2 sets of www). At the end of your target, you have family-disease%1, which means if start=20 then the end of your URL will look like: family-disease20. Is that right?
The new URL doesn't have the query string in it, so it is just stripping of the last URL path part. If you want it hardcoded
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease? [R,L]
or a little bit more flexible
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/.+$ /en/family-members/family-disease? [R,L]
or if you just want to keep two levels after en/wfmenuconfig
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/(.+?/.+?)/ /en/$1? [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
If you just want to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease, then you must try these directives:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease [R=301,L]
But if that's not what you want, but to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease$var then you could check this one:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease%1 [R=301,L]
Now, give this one a little more try if it will work. If it's not, then find any suspicious why this code is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /en/
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^wfmenuconfig/family/family-disease/177-category-english /family-members/family-disease [R]
And go to http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$AnyNumber if it's redirecting into http://www.mydomain.com/en/family-members/family-disease just make sure that your web server have mod_rewrite.
I just wanted to throw this out there, I was also having trouble getting the RewriteRule to work. I have a client that upgraded to a WordPress powered site from .asp pages. What I had to do to get this to work is insert the RewriteCond and RewriteRule in the htaccess file BEFORE the "# BEGIN WordPress" section. Now it works just as it should.
This is posted way late, but hopefully it helps someone else out there running into the same issue.
Doesn't Work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
Does Work:
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Order of operations must be important =)

.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 - RewriteRule

Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks

Resources