After a day or two, I am still fighting with Mod Rewrite. It seems that 60% of my development time is spent battling the server.
My current URL structure is such that all http://example.com/xyz and http://example.com/xyz/abc should be handled by index.php. The problem is I have a http://example.com/admin/ section, which is a real directory which I need to be accessible via HTTP request (it's the CMS directory)
When I try to browse to the CMS http://example.com/admin/, It changes my URL to http://example.com/admin/?n=admin and returns a 404. My index.php is receiving n=admin as it's argument.
What I cant understand is why these two conditions are being ignored:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^admin(?:\/)?$
And why I'm getting that redirect to http://example.com/admin/?n=admin (Rather than just stopping at http://example.com/admin/.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^admin(?:\/)?$
# allow access to certain subdirectories.
RewriteRule ^admin(?:\/)?$ /admin/ [L,NC]
# redirect all old URLs to new pages (or 404 sitemap page if no analog?).
RewriteRule ^company/about(?:\/)?$ /company [R=301,L,NC]
# catch any others and try to serve them right
RewriteRule ^/?(.+).html$ /$1 [R=301,L]
RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]
Can anyone offer any ideas or point out any flaws in the .htaccess?
Each RewriteCond only applies the the next RewriteRule.
so only your first Rule has a condition.
Your options are to repeat the rules, or make then final and exiting (cannot remember flag for this off my head)
Try this rule before your other rules:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This will end the rewrite process if the requested path can be mapped to an existing directory. The same applies when using -f instead.
Related
We're currently upgrading our site forum software, and need to redirect all old forum thread URLs to the new format.
We've tried a couple of things but can't get this to work, as different .htaccess redirect rules are conflicting.
Old format:
https://www.example.com/beta/news-and-announcements/1354-thread-name.html
New format:
https://www.example.com/beta/threads/thread-name.1354/
We would like these to 301 redirect to the new URL structure to ensure everything keeps working.
news-and-announcements in the first URL is a dynamic sub-forum name.
1354 is the thread ID - this is really the only bit that needs to be kept and moved over to the new URL.
thread-name - forum thread name - as long as the ID is in the correct place this will get rewritten to be correct by the new forum software.
This is the default .htaccess mod_rewrite section:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
We've tried to expand this to cover some additional redirects from the old URL structure like this, but it's not working:
RewriteBase /beta/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)\/(\d*)-(.*).html$ threads/$2 [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
The above redirect doesn't work, and also breaks the AdminCP URLs somehow:
https://www.example.com/beta/admin.php
Any help on getting this working would be hugely appreciated!
test it!
RewriteRule ^news-and-announcements/(\d+)-(.+).html /threads/$2.$1/? [R=301,NC,L]
add this codes below RewriteEngine On on .htaccess file
Alright, this is due to the pain that godaddy gives me by implementing their own caching in a MANAGED WORDPRESS hosting. I looked it up and as it turns out, their flush caching facility is not available to me in the wordpress dashboard as it is a subdirectory /wp/ installation.
Also, there is no setting to enable "development mode" which apparently turns off caching on the godaddy control panel.
But what I would like to do is to apply ?nocache=1 to every URL related to the site (including the assets like style.css) so that I get the non cached version of the files.
For example, if I put
http://example.com/wp/wp-content/themes/example-theme/style.css
in the browser, I get a cached version of the stylesheet which does not reflect the recent one. But if I put..
http://example.com/wp/wp-content/themes/example-theme/style.css?nocache=1
It shows up the most recent one.
Is this possible? If yes, could someone be kind enough to show me how? Perhaps Something like this... (it doesn't work!)..
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1?nocache=1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress
Thanks for your help!
Try these rules to add required query parameter:
RewriteEngine On
RewriteBase /wp/
RewriteCond %{QUERY_STRING} !(^|&)nocache= [NC]
RewriteRule \.(?:jpe?g|ico|gif|bmp|png|tiff|css|js)$ %{REQUEST_URI}?nocache=1 [R=302,L,QSA,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
They offered a code to disable it directly into your wp-config.php file.
Basically just add:
define( 'MWP_OBJECT_CACHE_DISABLED', true);
https://www.godaddy.com/help/object-caching-on-managed-wordpress-41408
I am trying to structure a rewrite rule to implement redirection based on languages.
My directory structure is as follows:
.
├── .htaccess
├── assets
│ ├── css
│ │ └── master.min.css
│ ├── fonts
│ │ ├── fontawesome-webfont.eot
│ │ ├── fontawesome-webfont.svg
│ │ ├── fontawesome-webfont.ttf
│ │ ├── fontawesome-webfont.woff
│ │ ├── fontawesome-webfont.woff2
│ │ └── FontAwesome.otf
│ ├── img
│ │ ├── logo.svg
│ │ ├── slide1.jpg
│ │ ├── slide2.jpg
│ │ ├── slide3.jpg
│ │ └── slide4.jpg
│ └── js
│ └── scripts.min.css
├── de
│ ├── index.php
│ ├── sie.php
│ ├── uns.php
│ └── zusammen.php
├── en
│ ├── index.php
│ ├── together.php
│ ├── us.php
│ └── you.php
├── fr
│ ├── ensemble.php
│ ├── index.php
│ ├── nous.php
│ └── vous.php
└── it
├── index.php
├── insieme.php
├── noi.php
└── voi.php
I have no root index.html or index.php file. I want to have the .htaccess redirect the user to one of the index files inside the language directories by sniffing the browser language and then redirecting the user to the appropriate language. The default language, when the browser's language cannot be sniffed should be French.
My current .htaccess file consists of the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de|fr|it)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^(.*)$ /fr/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /it/$1 [L]
This seem to function partially, but it isn't able to access the various assets such as images, javascripts, or fonts.
Once the page loads, thereafter, there shouldn't be any problems navigating and changing languages as I have used internal URLs that are directly linking to the specific pages in the appropriate directories.
Any help would be appreciated. Thank you.
You can detect and route using browser's languages. Place this code in root .htaccess
RewriteEngine On
RewriteRule ^(assets|en|de|fr|it)/ - [L,NC]
# detect browser language and capture first 2 chars
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# check if corresponding directory exists
RewriteCond %{DOCUMENT_ROOT}/%1/ -d
RewriteRule ^ %1%{REQUEST_URI} [L]
## default fr rule
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# current request is not pointing to a real directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/?$ fr%{REQUEST_URI} [L]
I don't know how you try include assets in you source code, So you can try make this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(img|css|js|fonts)/(.*)$ assets/$2/$3 [QSA,L]
RewriteRule ^(.*)/(fr|it|en|de)/(.*)$ $2/index.php?area=$3 [QSA,L]
RewriteRule ^.htaccess$ - [F]
i have a special situation and i can't find a good solution. I've already seen dozens of questions/answers here but none of them seems to solve my problem!
I have an url like this:
https://subdomain.domain.com/{user-name}/{app-name}/
"user-name" and "app-name" can change everytime and i need to redirect it to
index.php?u={user-name}&a={app-name}
But if the url is only https://subdomain.domain.com/{user-name}/ i need to redirect it to
store-list.php?u={user-name} (to show a list of all available apps for that user).
My .htaccess is currently like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
It redirects perfectly on both situations but every call to a "real" file doesn't work (for example a call to a js or css file inside my html).
What am i doing wrong??
Making one RewriteCond Set Apply to Several Rules
Yes, we're really close... but a RewriteCond only applies to one rule. That's what is throwing us off. Let's use some tricky logic and put the conditions in reverse:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,S=2]
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
The conditions say that if the files do exist, leave them unchanged, and to skip the two next rules (S=2).
I have a really basic rewrite rule that I've been banging my head against the wall trying to achieve. I have a static HTML site that I want pretty URLs for.
So, I want /something to serve /something.html and I also want to redirect (externally) from /something.html to /something as to not be penalized on SEO for hosting duplicate content.
I don't want to use Multiviews and I don't want to use <rel cannonical="">
This is what I currently have.
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
It results in an infinite redirect loop.
It seems to me that the first rule keeps matching even after the requested URL doesn't end in .html.
Yes your rules will cause redirect loop due to use of REQUEST_URI (via RewriteRule) which changes after application of a rule.
You can use:
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.