htaccess redirect pages that don't exist

htaccess redirect pages that don't exist - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the htaccess redirect pages that don't exist error on your web browser. Problem :


I have a website that has a little over 100 members. Each member gets a replicated website, and by default the software we use sets up their replicated website at a url like:



domain.com/?username



"username" being the username they registered with. What I'm wanting to do is allow the users to access their replicated website without having to type the question mark before their username. For example, I would want domain.com/username to redirect to domain.com/?username.



I tried adding the following to my htaccess file:



Redirect 302 /username domain.com/?username



This doesn't work, it just gives a 404 not found page when trying to access domain.com/username because it doesn't actually exist.



Basically, I just want my users to be able to access their websites without having to type the question mark. I hope I've made what I'm trying to accomplish clear.



Is this possible?



Any help is greatly appreciated.



Thanks.


Solution :

Well, if you literally used the following rule:



Redirect 302 /username domain.com/?username


then it probably tried to redirect you to http://domain.com/domain.com/?username - because you didn't provide an absolute path (with my Apache 2.4.6 this rule even returns an internal server error). The following should work:



Redirect 302 /username http://domain.com/?username


But this only works for username, so if you don't want to add this rule for every user in your database, you could make use of the RedirectMatch statement:



RedirectMatch 302 /([a-zA-Z0-9_-]+)$ http://domain.com/?$1


As you see, I capture the username with the pattern ([a-zA-Z0-9_-]+) - so if your usernames can contain other characters, you'll have to modify that pattern. But beware: with this method your username-patterns must not contain dots. The reason being, that this rule would then match for "normal" filenames like index.php or header.png, which would break your site.



You could solve that by prepending a virtual directory like /u/ before the username. Instead of http://domain.com/username it would look like http://domain.com/u/username. The RedirectMatch-rule for that would look like:



RedirectMatch 302 /u/([a-zA-Z0-9_-.]+)$ http://domain.com/?$1


You still want to limit the characters in your pattern because an username like username&admin=1 sets two $_GET-elements: "username" => "" and "admin" => "1" - which is probably not what you would be expecting.



There are other solutions using mod_rewrite which might be cleaner (some of them would require you to change your PHP-scripts). If you can use mod_rewrite make a comment and I'll add some examples if you like.


We hope that this article has helped you resolve the php, htaccess, apache error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=