The overall goal is to create a twitter-like site where if you try to go to any of the pages that are not indicated, you will be taken to a profile page instead. While this is somewhat simple in most cases it becomes a bit more complicated inside of Joomla due to the fact that the !-f command is a bit useless. Article titles don't actually exist so this one does not.
The only alternative that I have functioning is to list every page I don't want to redirect as a condition. The result is something like this. The Custom redirects is the section in question.
# Use PHP5.3 as default
AddHandler application/x-httpd-php53 .php
RewriteEngine on
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
## Mod_rewrite in use.
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !how-it-works
RewriteCond %{REQUEST_FILENAME} !plans-and-pricing
RewriteCond %{REQUEST_FILENAME} !contact-us
RewriteCond %{REQUEST_FILENAME} !login
RewriteRule ^(.*)$ ./index.php?option=com_profiles&view=publica&Name=$1 [R=301]
## End - Custom redirects
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
Now this works perfectly with one minor hitch. It appears that the css for this page is actually using the referring page's. Joomla is made modularly so the template calls for a content section and the content section is being replaced while the rest is not. This includes header files linking to style sheets.
I was hoping some of the people here might be able to take a look at this and tell me what is preventing this from being a proper redirect. I don't really use htacess so some of the nuances are lost on me.
Related
At root there is WordPress site. And in subdirectory calles 'vue' there is vue build is there. In wordpress URL if 'podcaster' or 'crowd' is not a part of a URL then I want to load vue page without changing browser URL.
Below is my folder structure
Target is:
www.example.com/podcaster //display in the address bar, points to WP (var/www/html/)
www.example.com/crowd //display in the address bar, points to WP (var/www/html/)
www.example.com/username //display in the address bar, points to VUE (var/www/html/vue)
www.example.com //display in the address bar, points to WP (var/www/html/)
Below is the WordPress root directory .htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrite rule for vue
RewriteCond %{REQUEST_URI} !^/crowd/
RewriteCond %{REQUEST_URI} !^/podcaster/
RewriteRule (.+) /vue/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Below is vue sub directory .htaccess:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
You've not stated exactly what the problem is, however...
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteRule ^(.*)$ /vue/$1 [L,NC]
This will result in requests for the root being internally rewritten to the /vue subdirectory (I'm assuming you've placed this at the top of the WordPress .htaccess file in the root directory). You've stated that WordPress should be served from the root. In which case you should change the RewriteRule pattern from .* (0 or more) to .+ (1 or more) to avoid being triggered for requests to the root (base URL / homepage).
You will also need to ensure that rewritten requests (by the WordPress front-controller - to index.php - in the code that follows) are not also rewritten (otherwise everything will ultimately be rewritten to the /vue subdirectory). We can do this by adding another condition that checks against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to 200 (as in 200 OK status) after the later rewrite.
UPDATE: And I suspect you also have a number of static resources (CSS, JS, images, etc) that also need to be excluded, so we probably need to add a filesystem check for those, so the request is not rewritten if it already maps to a file (or directory).
For example, bringing the above points together, this becomes:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /vue/$1 [L]
I've removed the NC flag as that was superfluous here. Note also that the condition is successful when the string "/podcaster/" (note that slashes) does not appear anywhere in the URL-path. Should this not be restricted to the start of the URL-path perhaps? eg. !^/podcaster/.
Also, where is the rest of the WordPress .htaccess file? The WordPress front-controller (the part that normally appears after the # BEGIN WordPress comment marker) should appear after your custom directive. If you place your custom rewrite at the end of the WordPress .htaccess file, after the WP front-controller section then your directive will not doing anything since all requests will be routed to WordPress.
Note that you should place your custom directives before the # BEGIN WordPress comment marker. You should never edit the code between the # BEGIN WordPress and # END WordPress comments since this block of code is maintained by WordPress and your code could be overwritten when WP updates (unless you take additional steps to prevent this). There is no need to repeat the RewriteEngine or <IfModule> directives.
Below is vue .htaccess which in "vue" sub directory
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vue/
RewriteRule ^vue/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vue/index.html [L]
</ifModule>
The Vue .htaccess file isn't quite right (although should work OK). The first RewriteRule directive (which is simply an optimisation) should not include the vue subdirectory in the regex. In other words, it should be written:
RewriteRule ^index\.html$ - [L]
Since the URL-path that is matched is relative to the filesystem path that contains the .htaccess file.
In fact, you can remove all instances of vue from this file (which makes it simpler and more portable), providing you also remove the RewriteBase directive entirely. For example:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Relative substitution strings (ie. index.html in the 2nd RewriteRule directive above) are relative to the directory that contains the .htaccess file (providing you don't have a RewriteBase directive that states otherwise). So, the above naturally rewrites the request to /vue/index.html without having to explicitly state the directory.
I have two pages on my Wordpress site that need to be served as https; the rest is vanilla http. Pushing the whole site to https would involve a lot of database editing to avoid mixed content, so I'm looking to write an htaccess script to use a RewriteRule that will make the change just for those two pages.
The browser addresses for the two pages are like
http://example.com/draw/membership-login/ and
http://example.com/draw/membership-join/membership-registration/
Note they both contain the word 'membership' and no other page does (or so I believe), so I thought I'd write a rule to look for any REQUEST_URI containing 'membership'. I did this and put it before Wordpress's own script in .htaccess (.htaccess is in directory draw, the home directory for the Wordpress installation). It looks like this (only the first 4 lines are mine; the rest is WordPress's, except I've commented out their RewriteEngine On).
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} membership
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302, L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
#RewriteEngine On
RewriteBase /draw/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /draw/index.php [L]
</IfModule>
# END WordPress
Result? All pages serve ok except the two membership pages, which serve as 404.
I guess it's good news in a sense, as the RewriteCond must be finding the membership pages, but the RewriteRule is clearly not delivering what I had hoped.
Any help much appreciated.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} membership(.+)
RewriteRule ^membership(.+) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The (.+) will match any character one or more times. A 301 redirect means that the page has permanently moved to a new location. A 302 redirect means that the move is only temporary.
I have a kind of Joomla and WordPress mashup.
The Joomla site is in the web Root directory (httpdocs directory) and the WordPress site is in a sub directory (jobs subdirectory).
I have a PHP script that uses a long query string placed in the WordPress directory:
http://example.com/jobs/search.php?search=true&auth=AUTHCODE&skills=developer&andor=OR×cale=6&areas=&andor_areas=OR&posted=1
I would like this to be rewritten to something like:
http://example.com/jobs/search/true/AUTHCODE/developer/OR/6//OR/1
I have tried many mod_rewite rules using the mod_rewrite generators online and they do not work. I get "page not found" from the standard WordPress warning.
I have tested mod_rewite rules both in the Joomla .htaccess file and the WordPress .htaccess file.
I have tried various different combinations of the code below using:
RewriteRule ^/jobs/cvsearch/
or
RewriteRule ^cvsearch/
or
RewriteRule ^/cvsearch/
at the beginning of the rule with no luck.
An example of what I have tried is below, in the WordPress .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /jobs/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /jobs/index.php [L]
RewriteRule /jobs/search/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ /jobs/search.php?search=$1&auth=$2&skills=$3&andor=$4×cale=$5&areas=$6&andor_areas=$7&posted=$8 [L]
</IfModule>
Joomla .htaccess file is below (shown as it comes with Joomla package):
##
# #version $Id: htaccess.txt 10492 2008-07-02 06:38:28Z ircmaxell $
# #package Joomla
# #copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
# #license http://www.gnu.org/copyleft/gpl.html GNU/GPL
# Joomla! is Free Software
##
#####################################################
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
#
#####################################################
## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks
#
# mod_rewrite in use
RewriteEngine On
########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to set a mosConfig value through the URL
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
#
########## End - Rewrite rules to block out some common exploits
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root)
# RewriteBase /
########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section
The directory part of the URLs in the RewriteRule is the confusing part for me as I am not sure on how it should be constructed.
Also, could the Joomla or WordPress .htaccess rules be affecting the mod_rewrite that I am trying to attempt?
You placed your mod_rewite rule too low, it should be right after your RewriteBase /jobs/.
The caret symbol was missing from the mod_rewrite rule and has been added into my supplied Apache mod_rewrite .htaccess code. What you had was this: RewriteRule /jobs/search/ etc. but should have been this: RewriteRule ^/search/ etc.
Since you already declared the RewriteBase as /jobs/, RewriteRule /jobs/search/ etc. is not necessary and has to be changed to simply using RewriteRule ^/search/ etc. minus the /jobs/ part.
Additionally, I added in a temporary 307 re-direct, non case sensitive rule to the mod_rewrite rule.
The Apache mod_rewrite .htaccess Code
RewriteEngine On
RewriteBase /jobs/
RewriteRule ^/search/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*) /jobs/search.php?search=$1&auth=$2&skills=$3&andor=$4×cale=$5&areas=$6&andor_areas=$7&posted=$8 [R=307,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /jobs/index.php [L]
I recently switched over my website to Wordpress. My old files were www.example.com/about.php and now its www.example.com/about/. I need to redirect incoming links from the .php extension to just the / for ALL my pages preferably using .htaccess.
What I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Whats in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I guess I don't know where I'd put it in my .htaccess file either.
Thanks!
The code you have does not redirect anything. It takes a request that might be for a php file and internally appends the .php extension. Nothing happens on the browser because you've not told it to do anything. This is a 2 step process here. See the top part of this answer for a short explanation.
In order to redirect, you need to match against the incoming request, not the URI (which could have been rewritten be previous rules or iterations):
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /(.*)\.php($|\ )
RewriteCond %{REQUEST_URI} !wp-admin
RewriteRule ^ /%2/ [L,R=301]
So when someone types http://www.example.com/about.php in their browser's URL address bar, the request will look like:
GET /about.php HTTP/1.1
and the %2 backreferences about and redirects the browser to http://www.example.com/about/ (note the trailing slash) and the address bar changes.
What happens then is the browser makes ANOTHER request but this time for http://www.example.com/about/ and the server gets the URI /about/. Now you need your rule to internally rewrite it back to the php file. Unfortunately, your rule doesn't handle the trailing slash, so you need something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
These would all go before your wordpress rules. The wordpress rules route everything to index.php and that would wreck any URI you are trying to rewrite.
You are doing things the wrong way around. You are actually rewriting urls that end end without php to files that do end in .php.
You'd need to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).php$ $1/ [NC]
I also removed the check if the file you are redirecting to exists as well as the [L] flag, because it's likely that wordpress is also doing its own rewriting, which means that you don't want this to be the last rule processed and that you won't be able to find the file on the filesystem.
Are all requests handled in index.php?
Yes. All* requests will go through index.php there is a rewrite rule in the .htaccess file which masks this and gives user friendly urls.
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
If Drupal can't invoke these rules then you will see index.php in the browser URL.
**There are cron.php and update.php which don't but these are special files for admin so are not part of the run of the mill site.*
Yes. If you're looking for certain code snippets that handles URL parsing and calls various modules then take a look inside bootstrap.inc