Remove query param with URL Rewriting not working - .htaccess

I'm trying to rewrite the URL's with params contained looks like: https://mywebsite/FOF/news/page/page1.php?name=petter&country=usa
I think it's not very aesthetic as url and I would like it to look more like this:
https://mywebsite/spiderman
Actually the rewrite work and give me this result
https://mywebsite/spiderman?name=petter&country=usa until https://mywebsite/spiderman
This is my redirected rules
##Enabling FollowSymlinks and disabling MultiViews options here.
Options +FollowSymlinks -MultiViews
##Setting rewrite base to / here.
RewriteBase /
RewriteEngine on
##Rewriting for page1 uri here.
RewriteRule ^page1$ page1.php
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/FOF/news/page/page1\.php\s [NC]
RewriteRule ^ /customPage? [R=301,L]
##Internal rewrite rules here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customPage/?$ FOF/news/page/page1.php [L,NC]
if someone can help me

With your shown samples, attempts; please try following htaccess Rules. Where customPage is the page which you want to redirect on browser(spiderman in your shown question). This is for specific page request as per your shown samples.
Make sure to clear your browser cache before testing your URLs.
##Enabling FollowSymlinks and disabling MultiViews options here.
Options +FollowSymlinks -MultiViews
##Setting rewrite base to / here.
RewriteBase /
RewriteEngine on
##Rewriting for page1 uri here.
RewriteRule ^page1$ page1.php
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/FOF/news/page/page1\.php\?name=(?:[^&]*)&country=(?:\S+)\s [NC]
RewriteRule ^ /customPage? [R=301,L]
##Internal rewrite rules here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customPage/?$ FOF/news/page/page1.php?name=petter&country=usa [L,NC,QSA]

Related

Htaccess redirect to controller action don't work properly

I want to redirect to my contoller - SiteController.php and action actionLanguage. I think my htaccess is OK but can't reach the action. Everything that I am trying to put in the url leads me to admin/site/index and not to admin/site/language in my particular case. My .htaccess :
# prevent directory listings
Options -Indexes
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin/gii/?$
RewriteRule . backend/web/index.php?r=gii [L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]
RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1
This line RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT] must execute to backend/web/site/language when I type www.website.com/admin/site/languageright ? Or I am in some mistake? It redirects the request to the admin/site/index now. Appreciate every advice! Thank you in advance!
EDIT:
Backend .htaccess:
AddDefaultCharset utf-8
Options FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
RewriteRule ^static - [L]

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

How to fix a mod_rewrite RewriteRule in HTACCESS

I'm writing a few RewriteRules, and my most basic rewrite returns a 404 error. Here's my code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteRule ^submit/$ submit.php [R,L]
# Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
RewriteRule ^submit/$ submit.php [R,L] is where I'm having trouble.
When I visit domain.com/submit/, my server returns a 404 error saying, "The requested URL /submit/ was not found on this server." It's like the server did not even look at my HTACCESS file. The other RewriteRules work perfectly.
Am I missing something?
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteCond %{REQUEST_URI} !submit\.php [NC]
RewriteRule ^submit submit.php [R,L,NC]
That's it according to the information in the question.
Can't guess what's the first rule for as there are no URL examples to determine the pattern to be matched by the regex or the location of the script.
Something similar happens with the second rule because there is no way to confirm where the script (submit.php) is located as, again, there are no URL examples in the question.

Redirect All Requests to a Post

I'm trying to redirect all pages to a blog post at URL
example.com/big-changes-for-2013/
(including trailing slash)
I do not want to redirect me, because I'm working on the rest of the site.
This is what I have so far (this is a .htaccess redirect):
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/big-changes-for-2013/$
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteRule $ /big-changes-for-2013/$ [R=302,L]
The part preventing me from being redirected works. The part thats not working is the redirect itself, which is an infinite loop.
The code above is based off of a combination of this and this.
Any ideas?
Try
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteRule ^(?!big-changes-for-2013/$) /big-changes-for-2013/$ [R=302,L]
The rule regexp is what is a called a negative look-ahead assertion. It means "match anything other than big-changes-for-2013/$".
Solved with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REMOTE_ADDR} !^50\.137\.88\.129
RewriteCond %{REQUEST_URI} !^/(big-changes-for-2013/)
RewriteRule ^(.*) /big-changes-for-2013/ [L,R=301]
</IfModule>

mod rewrite clean url for search box

I have mod rewrite enabled to remove all page extentions....
This is done in httpd.conf as I am using apache on windows
the setup I have is
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
and this makes domain.com/bookings.html
appear as domain.com/bookings
I have php enabled in html files, so it parses all pages for php
But I have now put a search box on my site, and if I search for "music" it will use the url:
domain.com/search?q=music
I would like my urls to look like:
domain.com/search/music
But also, I would like to be able to type
domain.com/search/abba
And it would load the page "search.html" lets call it "search" and it would add the parameter ?q=abba , but then still look like the above example in the URL bar
I'm sure this can be achieved with mod rewrite but I am not sure how to phrase the expression.
Thanks in advance for any help I receive :)
use the kind of URLs in your html: domain.com/search/music
add this in your .htaccess file in your DocumentRoot.
Options +FollowSymLinks +Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
or add this to your .conf file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
You may just add one simple RewriteRule:
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
So all in all:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
</IfModule>
Oh by the way:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Resources