I've read through about 6 or 7 suggested similar post but still can't get this working.
(e.g.:
Reference: mod_rewrite, URL rewriting and "pretty links" explained /
How to write htaccess rewrite rule for seo friendly url
etc...)
I have a PHP website with a help articles section, which pulls the content from the db by article id.
Current URL format =
/sitename/help.php?h=69
I'd like to change the URL format to:
/sitename/help/69/title-of-the-article
Mod_rewrite is enabled on the server, and this seems to work:
RewriteRule ^help/([0-9]+)/?(.*)/?$ /sitename/help.php?h=$1 [NC,L]
/sitename/help/69/random-title loads the content for /sitename/help.php?h=69
However, some of the articles contain images, coming from the path /sitename/images/
The modified URL means the browser tries to call /sitename/images/example.jpg from sitename/help/69/images/example.jpg
The '69' is an unknown variable, (and that seems to break all the examples I've tried!), but should always be a 1, 2 or 3 digit number.
Please can someone help with the rewrite/regex query to remove the /help/{article id}/ from my image URLs?
With your shown samples and attempts please try following .htaccess rules file. Please make sure to keep your .htaccess rules file along with sitename folder(not inside it, alongside it). Also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitename/(help)/(\d+)/.+$ sitename/help.php?h=$1 [NC,L]
Related
Summary:
I've been asked to migrate a .asp site over to Wordpress, but I'm having some issues with the .htaccess redirects.
What I'm trying to Achieve:
Pages:
domain.com/Some-Page-On-My-Site.asp
redirects too
domain.com/some-page-on-my-site/
Posts:
domain.com/articles.asp?title=The-Greatest-Post-Title
redirects too
domain.com/blog/the-greatest-post-title/
End Goal
Stay consistent with default Wordpress permalink structure using lowercase.
Convert only the .asp URL requests to lowercase not the rest of the site.
I'm thinking that because of the *.asp pages redirect that it may be sucking in the articles.asp and converting it to /articles/ . So I swapped their spot in the .htaccess file.
I'm thinking that RewriteCond and Skip Flag is probably required for this to work. But could be wrong.
I'm glad I was able to get the Pages redirect to work, although I think there is just a little more to make these work better.
What I've tried
This converts URL Requests for old .asp pages to new URL Structure:
RewriteRule ^(.*)\.asp$ /$1/? [L,R=301]
These attempts do not convert URL Requests for old .asp posts:
# Try 1
RewriteRule ^articles\.asp?title=(.+)$ /blog/$1/ [L,R=301]
# Try 2
RewriteRule ^articles\.asp?title=(.*)$ /blog/$1/ [L,R=301]
# Try 3 - Not that this would probably work but was an attempt
RewriteCond %{QUERY_STRING} (^|&)title\=(.+)($|&)
RewriteRule ^articles\.asp$ /blog/$1/? [L,R=301]
I confirmed that I can do 1-to-1 redirects, but not similar to the Pages structure like above where I can type anything before .asp and it just removes the .asp
RewriteCond %{QUERY_STRING} (^|&)title\=Duck\-Feet\-Wine($|&)
RewriteRule ^articles\.asp$ /blog/duck\-feet\-wine/? [L,R=301]
Does anyone have some insight on this? or am I crazy to think that I can do this?
I've read about 100+ pages and I'm stumped, including stumped on how RewriteCond works. I happy that I was able to get the pages redirect to work, now to convert a query string to a static like link to work with Wordpress's lowercase permalink structure.
I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.
I'm still a bit fuzzy on the working of .htaccess, and I've looked around but I can't find anything to help this specific issue.
EDIT: I realize there are other questions that seem like they cover this issue, but I checked some and they didn't seem to offer any help I could understand, and I didn't want to hijack them with my own issues.
This is what I have:
Options +FollowSymLinks
#RewriteBase /
RewriteEngine on
RewriteRule /mp3/(.*) http://old.domain.com/mp3/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
As you can see from the last line, the string typed after the server name is actually a URL parameter and depending on that parameter, different content is pulled from the database and that page is displayed on the site.
The problem I'm having is that the client has a content page called "podcast", so they would go to site.com/podcast which should quietly redirect to site.com/index.php?=podcast and load the content for that page. Unfortunately, the client also has a real site.com/podcast/ folder on their server. Because of this, the rewrite is ignored and the browser attempts to load that folder. It either shows a file listing or a forbidden error if I disable the listing.
After some research (I'm still new to htaccess), I learned that the two lines prior disable the rewrite if the path points to an actual file or folder. Unfortunately, commenting out the one with !-d doesn't seem to have any effect, and commenting out both gives me a server error.
Admittedly, part of the problem here was lack of foresight. URL rewrites should have been planned before everything else was put together, but it wasn't until the site was basically completed that I was notified that the client wants "Friendly URLs" that don't include the ?p= part. Regardless, perhaps there is a way to fix this.
Is there some .htaccess trickery I can use that will force the rewrite even if the URL entered points to a folder (not a specific file) that actually exists? As stated before, removing the !-d doesn't seem to help, although I'm not sure why. Perhaps I misunderstand its purpose.
Thank you for any help, and please be lenient with me if I overlooked something obvious. This is an issue presenting itself on the client's live site right now so I feel a little rushed in solving it. Thanks again.
OH YEAH, and the solution can't be specific to /podcast. The way the client's site is set up, when they want to create a new subpage for the site, a new name is saved for that content based on their title for the page and it is possible (unlikely, but still possible) that another page can be created with a name that matches an existing folder on the server.
Here is a note from mod_rewrite documentation:
By default, mod_rewrite will ignore URLs that map to a directory on
disk but lack a trailing slash, in the expectation that the mod_dir
module will issue the client with a redirect to the canonical URL with
a trailing slash.
This explains why mod_rewrite ignores the URL /podcast. I would suggest that you rename physical directories so that do do not (accidentally) match article names.
Another option would be to disable the DirectorySlash setting. This will prevent Apache from redirecting /podcast to /podcast/.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
Be warned that disabling this setting has side effects. Read through the documentation first.
Change the following line of code:
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
to
RewriteRule ^(podcast([^?]*)) index.php?p=$1 [L,NC]
On our website using Joomla we have duplicate URLs for the same page being found by Google Webmaster Tools. For instance the following URLs go to the same page on our site:
/lawson-equipment/auxiliary-equipment/poly-pro-sinks?option=com_content&view=article&id=75&Itemid=74
/lawson-equipment/pre-press/poly-pro-sinks/index.php?option=com_content&view=article&id=75&Itemid=74
/technical-support/digital-learning?option=com_content&view=article&id=75&Itemid=74
/lawson-equipment/textile-equipment/dryers/encore-dryer
with the fourth URL being the actual path I want. How would I go about formulating a rewrite rule that would grab any URL query strings with "&id=75" to be directed to the SEF URL without doing a Redirect 301 for each of the incorect URLs? This happens often.
Success! Using a RewriteCond command with a common snippet from the non-SEF addresses, and a RewriteRule command for the target address using regular expressions I come up with the following for my .htaccess file:
RewriteCond %{QUERY_STRING} ^.*com_content&view=article&id=75&Itemid=74$
RewriteRule ^index\.php$ http\:\/\/www\.mysite\.com\/lawson\-equipment\/textile\-equipment\/dryers\/encore\-dryer? [R=301,L]
So far this seems to be working and hasn't affected anything else that I can see. Please feel free to comment or add another answer if there is a better way of doing this!
I have inherited a custom ColdFusion CMS app. The URL's that it creates are horrendous. Not at all suitable for SEO or readability for that matter. An example of a URL in this CMS is:
http://www.mysite.com/Index2.cfm?a=000003,000010,000019,001335
Basically, each level of hierarchy is stored in the database based upon that long string of comma separated values. So in the case of the example I used, that particular page is 4 levels deep in the CMS hierarchy.
Basically what I would like to see is a format similar to this
http://www.mysite.com/level-1/level-2/level-3/level-4
Is this possible? Any help would be greatly appreciated. For what it's worth we are using ColdFusion 6 at present time, but will be upgrading to 8 in the near future.
First of all, are you willing to have the index.cfm in the URL? Like: http://www.mysite.com/index.cfm/level-1/level-2/level-3/level-4 ? If not, then you'll need to be doing a rewrite to remove the index.cfm, but still allow CF to process the page. Your .htaccess would look something like this:
RewriteEngine On
# If it's a real path, just serve it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Redirect if no trailing slash
RewriteRule ^(.+[^/])$ $1/ [R=301,L]
# Rewrite URL paths
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ /index.cfm%{REQUEST_URI} [PT]
Next step, you'll need to "catch" the URLs and serve up the correct pages based on the SEO-friendly URLs. You can grab the incoming URL from the CGI.path_info variable. It's hard to know what your code should look like without knowing how it currently processes those URL variables, but essentially you'd have some kind of mapping function that grabbed the SEO-friendly names and substituted in the numbers to grab the content.
The third step is rewriting any URLs that are generated by your CMS to output the SEO-friendly URLs. Same mapping happens here, only in reverse.