I am working on a php redirect script which 302 redirects visitors to other sites when they access a redirect url..
The script gets a variable (id) from the url and then redirects the visitor to the specific page.
The url structure is : example.com/redirect/index.php?id=test
At the moment all redirects work if I use "ugly" urls, but I want to strip all unnessecary information out of the url with .htaccess rewrites for better usability.
Which .htaccess rewrite rules do I need to make the above shown urls look like : example.com/redirect/test
I am currently using the following .htaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
but they only work for urls like example.com/redirect/index.php?id=test if I try example.com/redirect/test I get a 404 error page.
It might be good to know, that I have 2 .htaccess files, one in my root directory and one in the root/redirects/ directory.
Best regards !
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/? [NC]
Related
I'm trying to create a dynamic system on my PHP project and this is what my .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^pages/?$ resources/pages/page.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)?$ resources/pages/post-or-category.php?tcs=$1 [NC,L]
</IfModule>
/pages/ and dynamic post or category page works but the second rule redirects all if the above doesn't redirect. Since it's a PHP script for getting posts and categories, I don't want to make index check on that. Is there a way to rewrite if there is nothing on the domain (ex: www.domain.com or domain.com).
Also, I distinguish posts and categories in the script with the last world on URL (ex: P435345 for posts, C935943 for categories [numbers are id's]). Is it possible to check if URL's last word starts with P or C in .htaccess to redirect other URL's to 404?
Use directives RewriteCond before RewriteRule as this will not redirect if any file or directory exists. For eg. It will not redirect domain.com/index.php to resources/pages/post-or-category.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
I am trying to run a mod_rewrite rule to load the contents of index.php regardless of the entered URL.
Then in the index.php file I will deal with either returning a correct page or outputting a 404 page.
This is for SEO purposes to allow a nice URL like:
http://www.example.com/product
rather than
http://www.example.com/?p=product
I have this working on one sub folder of my blog but when trying to test it on a new project all i get is 500 errors as for ALL urls.
Here is the existing htaccess:
RewriteEngine on
RewriteBase /products/
RewriteCond %{HTTP_HOST} ^www.danclarkie.co.uk/products [NC]
RewriteRule ^(.*)$ http://danclarkie.co.uk/products/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/admin.php$
RewriteRule .* index.php
Any help ^_^
Dan
Ok, so I have to do some work on a client site - and for some reason, they have both the www. subdomain and the empty subdomain in use, with different websites. They do NOT want this.
They want the www. subdomain ONLY (that is, they want the empty subdomain to redirect to www.)
That's how it is set up in Wordpress, supposedly.
Here's my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)(\.htm|\.html)$ /contact [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#php_flag display_errors on
#php_value error_reporting 8191
Can anyone interpret this? (I am not very familiar with .htaccess files - anyone who has any resources on them would be helpful). How do I make it so that anyone going to http://examplesite.com goes to http://www.examplesite.com, for example.
How are there two different websites on the same domain? I'm looking in the public_html folder and there appears to be NO reference to the other site at all - nothing, no files, nada.
To force using www subdomain, you can add this code :
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [QSA,L,R=301]
Here is what those line does :
Check the HTTP_HOST, which must be domain.com
Take the current request and add it after www.domain.com/ ($1 represents the request caught)
Use the generated url as a 301 redirection (permanent redirection) with [R=301] tag
Doesn't read next rules if this one is caught, with [L] tag
Forward GET datas, with [QSA] tag.
You must put this code after those two lines :
RewriteEngine On
RewriteBase /
Which set the Rewrite engine on, and define the base url.
Be careful : redirection doesn't forward POST datas. Be sure alwayse use www subdomain in your code (this can easily be done using a centralized base_url).
Have a look here to understand better htaccess files.
Some... fine person... has set up an incoming link to our site that looks like:
http://www.site.com/*our*-*services*/
I'd like to redirect it so it points it to:
http://www.site.com/our-services/
Ours is a Wordpress site so there's some rewrite stuff in our root htaccess file already. A rule that simply removes asterisks from the URL would do, but I can't figure out how to do that, so I tried the following - loosely based on copying the existing Wordpress rules - which isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/\*our\*-\*services\*/)
RewriteRule . /our-services/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It looks like this rule is being ignored - the errant URL currently redirects you to our default Wordpress 404 page. Clearly, I am fail: what should I have put as the rewrite condition?
I'd remove your first RewriteCond and then just use this rule:
RewriteRule \*our\*-\*services\*\/ /our-services/ [L]
Well get them to change there incoming link.
Asterisks are reserved in urls, so in this case it shouldn't be used:
W3 Url Recommendation
Allowing asterisk in URL
I am updating an old asp site to cakephp - the old site has various listings on google based on the old "filename.asp" urls - I'd like to put Redirect 301s in the htaccess file to try and hang on to those search results (most of the pages have a complementing page on the new site), but something appears to be going wrong. htaccess as follows (excluding standard cake stuff). What am I doing wrong?
Redirect 301 contact.asp /contact
Redirect 301 portfolio.asp /portfolio-design-web
Redirect 301 webhosting.asp /
I've tried with the htaccess in the root directory, and webroot but it should just work wherever, no?
--
fixed it using mod_rewrite, following rules inside .htaccess on webroot work:
RewriteRule ^contact.asp$ /contactos/ [R=301,L]
Try modifying app/webroot/.htaccess like so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^contact.asp$ /contact [R=301,L]
RewriteRule ^portfolio.asp$ /portfolio-design-web [R=301,L]
RewriteRule ^webhosting.asp$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>