.htaccess Mod Rewrite code? - .htaccess

I have searched for this but nobody have the same issue..
Now I need an code to transform the ugly url (thats what i have) to the nice url:
Ugly url: domain.com/aanmelden?referral=admin
Nice url: domain.com/aanmelden/admin
Ik have tried soo many codes, but no one did work for me. ):
suggestions?
Thnx!
(My current htaccess: )
ErrorDocument 401 /errordocs/401.php
ErrorDocument 403 /errordocs/403.php
ErrorDocument 404 /errordocs/404.php
ErrorDocument 500 /errordocs/500.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.nl$ [NC]
RewriteRule ^(.*)$ http://domain.nl/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
Where and what i need to place?
EDIT:
To be clear:
If the user goes to the Nice Url, they wil stay on the nice one.
If the user goes to the Ugly Url, they wil redirect to the Nice url.

This is a trivial rewrite:
If admin can be any lowercase string, use the pattern ([a-z]+)$ to capture one or more lowercase letters after the / and before the end of the request URI.
RewriteEngine On
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
If admin is really only admin, you can hard-code it as
RewriteEngine On
RewriteRule ^aanmelden/admin aanmelden?referral=admin [L]
Update
In context of your existing rewrites just posted, you'll need to add this rule before the rule that adds .php since that would also match this pattern.
# Do this before the rule that adds .php
# Also added condition so this doesn't apply to real files...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^aanmelden/([a-z]+)$ aanmelden?referral=$1 [L]
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Update 2
If you want the end user to get the ugly URL and rewrite it to the nice one, use:
RewriteCond %{REQUEST_QUERYSTRING} referral=([a-z]+)
RewriteRule ^aanmelden aanmelden/%1 [L]

Related

ht-access how to redirect to a forum topic url?

I would like to know how to use ht-access to redirect my pages which look as follows .com/?page=forums&topic=topic-name to be changed to .com/forums/topic-name?
The rule below redirects all the page names for example .com/forums or .com/polls which i currently use to get my pages to .com/index.php?page=forums.
RewriteRule ^(.+)$ index.php?page=$1 [L]
i would like www.treyarder.com/home to redirect to www.treyarder.com/index.php?page=home as well as www.treyarder.com/forums/topic-name to redirect to www.treyarder.com/index.php?page=forums&topic=topic-name
my topic is at www.treyarder.com/index.php?page=forums&topic=topic-name
my ht-access file as shown below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L]
RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]
ErrorDocument 404 /index.php?pages=404
ErrorDocument 500 /index.php?pages=500
there seems to be something wrong with the below line
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
does anyone know what it may be? as if i can fix that line then this should hopefully solve my question.
You must remove both ErrorDocument lines from your .htaccess as you're handling all non-file, non-dir requests through your front controller pattern.
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&topic=$2 [L,QSA]
## COMMENT OUT THESE 2 LINES
# ErrorDocument 404 /index.php?pages=404
# ErrorDocument 500 /index.php?pages=500
css/js/images issue: It is due to your use of relative links for these resources. Try ANY of these 2 work-arounds:
Use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /
Add this just below <head> tag of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.
okay this was the final outcome which we managed to mix and match until it worked.
RewriteEngine on
RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1 [L]

Giving error with dot in url

i have a problem with my url, when dot comes in url it gives an 404 error.
url: http://example.com/Samsung-Galaxy-Mega-5.8
i used some code in htaccess but it doesn't work properly.
RewriteRule ^([a-zA-Z_\-]+)/?([a-zA-Z0-9\-=&_#\.]*)$ /$1.php?$2 [QSA,L]
EDIT (copied from comment):
RewriteEngine on
Options -Indexes
#RewriteCond %{HTTP_HOST} ^([a-z0-9-A-Z_]*).([a-z]*)$
#RewriteRule ^(.*)$ %1.%2/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([a-zA-Z0-9_-]+)$ show_mobile.php?bid=$1&brandname=$2
RewriteRule ^([a-zA-Z0-9\-]+)-([a-zA-Z0-9\-]+)$ show_mobile.php?bid=$2&brandname=$1
I guess the following should get you going:
^([a-z0-9\-\.]+)\-([a-z0-9\-\.]+)$i
(based on the .htaccess from comment)
https://regex101.com/r/jZ9sP3/1
You didn't allow for periods and you didn't escape the hyphen inbetween the catching rules.
the tailing i is for case insensitive, so you don't need to do a-zA-Z

Rewrite rule htaccess disturbing other rewrite rules

I have a page on my website that is getting generated dynamically to list all outlets based on cityf parameter and below is rewrite rule to convert it into SEO friendly URL and it is working pretty well.
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL (http://example.com/title-of-blog)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
Now the problem here i am facing that when someone visits blog page then the link http://example.com/title-of-blog instead of displaying blog detail on the page, displays my Error message that No outlets near title-of-blog.
I got the issue that Apache is not able to identify when to rewrite cityres page and when to rewrite blogdetail page.
Someone suggested that Make sure that each rule has a common prefix (e.g. /blog/page1 and /news/page2). but i did not get that.
Any suggestions here please?
EDIT:
Whole htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
RewriteRule ^food-([^-]*)-([^-]*)\.html$ /pdetail?res_id=$1&location=$2 [L]
RewriteRule ^foodies-([^-]*)-([^-]*)$ /pdetail_new?res_id=$1&location=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /pdetail_ne?location=$1&res_id=$2&name=$3 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
Both your rules match the exact same pattern. Therefore, the first rule will always match and the second rule does nothing.
Looking at the first rule:
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
This matches http://example.com/title-of-blog as well as http://example.com/city-name
When you look at it, you can tell which needs to be handled by blogdetail and which needs to be handled by cityres, but the regex ([^/.]+) sees them both as exactly the same, and matches both. Your regex doesn't know the difference, so whatever the first rule is, both URL's will get matched by it.
Like you said, someone suggested using a prefix. That way, the regex knows which is which:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ANd your URLs will look like:
http://example.com/city/city-name
http://example.com/blog/title-of-blog
If you're really hung up about not adding prefixes, you can remove the second prefix:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So that you have:
http://example.com/city/city-name
http://example.com/title-of-blog
EDIT:
Your 500 server error is caused by the rules looping. You need to add a condition so that they won't keep matching:
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

Trouble with mod_rewrite, subdomain and codeigniter

I have a codeigniter installation at example.com/ci.
I have a subdomain foo.example.com. The document root for the foo subdomain is set to be home/public_html/ci.
I'm using the following rule in .htaccess to send requests for foo.example.com to example.com/ci/city/foo.
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L]
It all works like I want it to except that the address bar url changes from foo.example.com to example.com/ci/city/foo. I would like it to remain foo.example.com. There is no R=301 in the RewriteRule (used to be but I removed it). The .htaccess file is in the ci/ folder and the rule is above all the codeigniter stuff.
The redirect works perfectly and the url remains foo.example.com with (Jon Lin's answer)
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
but the codeigniter default controller is called instead of the foo method in the city controller.
Any help is appreciated.
When your rewrite rule's target has an http://example.com in it, a 302 redirect is implicit regardless of whether an R flag is used or not. You need to provide the URI path based on the subdomain's document root, so I'm assuming you want something like:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
If the subdomain's document root is in the /ci/ directory.
The other option is to use the P flag to reverse proxy the request:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L,P]
Your mileage may vary with this (might need to finesse it to fit your server and conditions), but doing some testing on my Mac, here's what I had mild success with:
Directory Structure
public_html/
ci/
application/
system/
.htaccess
index.php
I'm assuming that you have other stuff in your root public_html directory. So I'm letting the .htaccess focus on the CodeIgniter-related stuff by leaving it in the ci dir.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.ciwildsub\.dev [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/city/%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It's fairly self explanatory, but the first block is your subdomain check. I didn't bother excluding www but you may want to (as I said, your mileage may vary). The second block is a standard CodeIgniter index.php removal.
These rules will only apply to sub.example.com or example.com/ci/ URLs, since as I said, I assume your root has stuff that shouldn't be disturbed by rewrites.
CodeIgniter Config
$config['uri_protocol'] = 'PATH_INFO';
Because of the way Apache handles a URL like example.com/index.php/controller/method, it bypasses the index.php and handles it like any other directory segment. Also, mod_rewrite doesn't necessarily stop at the [L] tag -- it stops processing the .htaccess at that point, passes through the RewriteRule, and then runs that URL through the .htaccess. Setting PATH_INFO helps make sure CodeIgniter pulls the current URI correctly, and our .htaccess doesn't get stuck in a validation loop.
I will note, though, that I'm not entirely happy with what I see in my RewriteLog output -- there has to be a way to optimize this further, I'm just not sure of it yet (I'm done tinkering with this for today!). Sorry if any of the explanation here is a little out of whack - I'm not a server admin or mod_rewrite expert, I've just had fun tinkering with this. If I manage to find a better solution, I'll be sure to update this.
Looks like the END flag would be perfect for situations like this (to prevent [L] loops), but it's only available in Apache 2.3.9+. The search continues.
I got it to work correctly using the following rewrite rule
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
and by setting
$config['uri_protocol'] = 'ORIG_PATH_INFO';
in the codeigniter config file. Thanks for all the help.
This worked for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /file_path/to/subdomain
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

.htaccess redirecting errors

First off I am using the Codeigniter Framework so this issue is a workaround the way CI process URLs along with the current redirects I have set up using mod_rewrite.
I am trying to get a URL like this /?gclid=somestringgoeshere to redirect to /index.php?/home/gclid/somestringgoeshere.
The current .htaccess I have set is below
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This last condition enables access to the images and css folders, and the robots.txt file
# Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I am trying to use the following code right above the first set up rewrite conditions and rule's to catch it before it try's anything else
RewriteRule ^?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^\?gclid=(.*)$ index.php?/home/gclid/$1 [L]
and
RewriteRule ^/?gclid=(.*)$ index.php?/home/gclid/$1 [L]
All either don't show the correct page or come up with a 500 internal error.
The URI’s query can only be tested with the RewriteCond directive:
RewriteCond %{QUERY_STRING} ^gclid=(.*)
RewriteRule ^$ index.php?/home/gclid/%1 [L]
Or more general (will consider further query parameters):
RewriteCond %{QUERY_STRING} ^([^&]*&)*gclid=([^&]*)
RewriteRule ^$ index.php?/home/gclid/%2 [L]
Oh, by the way: RewriteCond directives only correspond to the first following RewriteRule directive.

Resources