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
Related
I have the below rewrite rules setup on a site. I' trying to set it up so I have the below different URLs.
Current .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^-]*)/$ ?action=$1 [L]
RewriteRule ^([^-]*)-([^-]*)/$ ?action=$1&id=$2 [L]
Required URLs
www.site.com
www.site.com/page/
www.site.com/product-1234/
www.site.com/privacy-policy/
The problem is that the second rewrite rule is affecting the privacy-polcy url but it shouldn't as the second rewrite rule is specific only to the product pages that have the product ID in it.
I'm also trying to ignore directories that exist as the structure of my site so under root I have the below directories which I don't want the rewrite rules to affect as the user shouldn't know anything about these directories.
/system/
/tasks/
# catch more specific urls:
RewriteRule ^product-([0-9]*)/$ ?action=product&id=$1 [L]
# ignore requests that want files or directories that do exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# then continue with the less specific:
RewriteRule ^([^/]*)/$ ?action=$1 [L]
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]
I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory
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