Complex .htaccess rewrite - apache

can you please let me know htaccess code to redirect there urls:
https://www.example.com/forum/news/state-news/archive to https://www.example.com/forum/news/state-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/archive to https://www.example.com/forum/news/state-news/city-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/area-news/archive to https://www.example.com/forum/news/state-news/city-news/area-news/?archive=yes
My tried htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css.php$ core/css.php [NC,L]
RewriteRule ^install/ core/install/ [NC,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?routestring=$1 [L,QSA]
</IfModule>

Have your htaccess Rules file in following way. I have specifically covered 3 URLs shown by you in your question. Please make sure you use these Rules and make sure you clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css\.php$ core/css.php [NC,L]
####I have commented below rule that doesn't make sense, since NO right part in it only condition is mentioned.
####RewriteRule ^install/core/install/ [NC,L]
###New rules added by me to handle 3 different URLs shown by OP in question.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(forum/news/state-news(?:(?:/city-news(?:area-news))))/(archive)/?$ $1?$2=yes [NC,QSA,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?routestring=$1 [L,QSA]
</IfModule>

Related

Renaming file in URL (.php file)

I have a file called filename-step1.php and looking to make the url domain.com/filename/step1 through .htaccess.
My current .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
At the moment, this works fine for removing the .php tag from the url. Just need to fix the directories.
You could add a RewriteRule to redirect to $1/$2.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /$1-$2.php [NC,L]
</IfModule>
So, filename/step1 will trigger the filename-step1.php file.
I am giving 2 solutions here, please try one at a time only in your .htaccess file. With your shown samples, could you please try following as a Generic rules.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1-$2.php [NC,L]
OR in case you are looking for specifically url filename/step1 try following.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteRule ^(filename)/(step1)/?$ $1-$2.php [NC,L]

Simple url Htaccess Redirection + youtube

Need help in forwarding below url with .htaccess rules
https://www.example.com/watch?v=yzwRsQ7rR-o
forward to
https://www.example.com/?url=https://www.youtube.com/watch?v=yzwRsQ7rR-o
I've following exisitng rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Check this redirect with query string
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^watch
RewriteCond %{QUERY_STRING} v=([A-Za-z0-9-]+) [NC]
RewriteRule (.*) /?url=https://www.youtube.com/watch?v=$1 [R=301,L]
</IfModule>

Codeigniter .htaccess not making my urls correct

I have following .htaccess file
<IfModule mod_rewrite.c>
# Checks to see if the user is attempting to access a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I am on localhost with WAMP installed on it. Normally this htaccess work perfectly but this time it isn't I have to mention index.php in my URLs. I have checked mod_rewrite is enabled in Apache. I just want to know what I missed
Try this...
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

.htaccess subdirectory redirect with non www to www and parameters

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
I tried to look for an answer, but haven't been able to find one.
I am trying to redirect:
http://example.com/qsg/abcd
to
http://www.example.com/qsg/index.php?Item=abcd
I can get this working fine when the www is present in the URI. When I remove the www, it redirects to http://www.example.com/404.shtml
I've tried the two methods above with both the same result. I just can't figure out what I am doing wrong. I've seen plenty of other examples where this should work, but not for me. Do I have something wrong in my .htaccess file is there a possibility of something else causing the bad redirect?
Note: both the above rewrites are not included in the file. I've tried both independently.
Give this one a shot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?Item=$1 [L,PT]
</IfModule>

Transparently rewrite/split specific url to different subfolder

I need to transparently split traffic to two different subdirectories.
For example any url with /api/...
Needs to be rewritten to app/webroot
Any other url needs to be transparently be mapped to the site/ subfolder.
I've tried several things attempting to just redirect everything to a single subfolder but can't even get that working, I have an eternal redirect loop.
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ site/$1 [L]
</IfModule>
Try this and see if this works for you.
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/api
RewriteRule ^(.*)$ /app/webroot [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]