URL rewrite conditions in htaccess file - .htaccess

I need some help with .htaccess. I've been using URL rewrites, but not conditions, and I'm pretty sure I need them seeing as I can't get this working.
The URL structure I need is -> forum/catnamehere (which shows the page called catnamehere)
and child structure -> forum/catnamehere/fornamehere (which shows the child page of the cat)
However, somehow I fail to do this, making me believe I need conditions.

You should be able to do this with two rules.
Put your .htaccess file in the forum directory, and add the following:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1&subPage=$2 [QSA]
The first rule will grab stuff like forum/abc123_
And the second rule will grab forum/abc123_/abc123_

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

Redirect the URL using .htaccess file

I have a page showing the products with the hyperlink for it as
www.domainname.com/productname
now my client needs to add store and needs the URL to show as
www.domainname.com/store/productname
I have done it via code and now when I click on it for a detail page, its still redirecting to
www.domainname.com/productname
but need to be
www.domainname.com/store/productname
tried with this:
RewriteRule ^store/?$ domianname.com/?$ [NC,L]
in .htaccess file, not sure whether I'm on page
Can any one tell me how to do it via .htaccess file.
Your RewriteRule is backwards: you need the path you're matching first, then the path you're redirecting to. Try this:
RewriteRule !^/store/(.*) /store/$1 [NC,L]
Also, you don't actually need mod_rewrite to do this. You could try mod_redirect, which is simpler and easier to understand:
RedirectMatch !^/store/ /store/
(N.B. I haven't tried either of these, so I'm not 100% certain they do what you want.)

.htaccess dynamic to static URL

I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

htaccess rewrite rule 'page numbers' problem

I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.
For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.
Below is my rewrite rule that I used for the initial category page:-
RewriteRule ^widgets$ products.php?cat=27
If any of you experts can help with this, it would be much appreciated.
Are you expecting the cat to change as well? You'd need to account for that in your URL as well:
e.g. www.site.com/widgets/27/2 could be rewritten as:
RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2
If widgets will always be cat 27 then you can change it to:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
which is query string append
Try
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
After that, go here :)
To allow a query string after your rewritten URL use the [QSA] flag:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
A link would than be:
http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
And I tried the following:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
This worked correctly but to go to page two of the widgets page, I need to type in the browser:-
/widgets?pg=2
But the actual 'page 2' link still leads to:-
products.php?cat=27&pg=2
So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.
The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

Resources